<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        div{
            width: 100px;
            height: 30px;
            background: darkcyan;
            position: absolute;
            
        }
        button{
            width: 50px;
            height: 25px;
        }
    </style>
</head>
<body>
    <button id="start">kaishi</button>
    <button id="end">tingzhi</button>
    <script>
        let start = document.getElementById("start")
        let end = document.getElementById("end")
        

        end.onclick = function(){
            arr.forEach(value => clearInterval(value))
            clearInterval(timer)
        }
        
        //获取数组 保存 位移的编号
        let arr = []
        let timer = setInterval(() => {
            let xfg = document.createElement("div")
            document.body.appendChild(xfg)
            
            xfg.style.top = "30px"
            xfg.style.left = "-150px"
            
            let left = -150
            let timer = setInterval(() => {
                left += 10
                xfg.style.left = left + "px"
            }, 30);
            arr.push(timer)
        }, 1000);
        
    </script>
</body>
</html>